package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Override
    public ResponseResult deleteById(Integer id) {
        //非空判断
        if (id == null) {
            ResponseResult.errorResult(AppHttpCodeEnum.NO_CONTENT);
        }
        this.removeById(id);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult list(WmSensitiveDto dto) {
        //判断参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if (dto.getName() != null) {
            wrapper.like(WmSensitive::getSensitives, dto.getName());
        }
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        page = page(page, wrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult sensitiveSave(WmSensitiveDto dto) {
        LambdaQueryWrapper<WmSensitiveDto> wrapper = new LambdaQueryWrapper<>();
        if (dto.getName() != null) {
            wrapper.eq(WmSensitiveDto::getName, dto.getName());
        }
        save(dto);
        return ResponseResult.okResult("保存成功");
    }

    @Override
    public ResponseResult update(WmSensitiveDto dto) {
        if (dto.getSensitives()!= null && !dto.getSensitives().equals("")) {
            updateById(dto);
            return ResponseResult.okResult("修改成功");
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_CONTENT);
        }
    }
}
